package com.tanhua.admin.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.commons.constants.Constants;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.FreezeDto;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/3
 */
@Service
public class UsersService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户管理-用户基本资料
     * @param userID
     * @return
     */
    public UserInfo getProfile(Long userID) {
        UserInfo userInfo = userInfoApi.selectById(userID);
        String key = Constants.USER_FREEZE + userInfo.getId().toString();
        String freezStr = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(freezStr)) {
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    /**
     * 用户管理-用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getProfiles(Integer page, Integer pagesize) {
        PageResult pageResult = userInfoApi.selectPageUserInfos(page, pagesize);
        List<UserInfo> userInfos = (List<UserInfo>) pageResult.getItems();
        for (UserInfo userInfo : userInfos) {
            String key = Constants.USER_FREEZE + userInfo.getId().toString();
            String freezStr = redisTemplate.opsForValue().get(key);
            if (StringUtils.isNotEmpty(freezStr)) {
               userInfo.setUserStatus("2");
            }
        }
        pageResult.setItems(userInfos);
        return pageResult;
    }

    /**
     * 冻结/解冻
     * @param freezeDto
     *      冻结：userId、freezingTime、freezingRange、reasonsForFreezing、frozenRemarks
     *      解冻：userId、reasonsForThawing
     * @param userStatus    0(冻结) / 1(解冻)
     * @return
     */
    public String freeze(FreezeDto freezeDto, String userStatus) {
        if (ObjectUtil.isEmpty(freezeDto)) {
            throw new BusinessException("网络异常，请稍后重试！");
        }
        //构造key
        String key = Constants.USER_FREEZE + freezeDto.getUserId().toString();
        if ("0".equals(userStatus)) {
            //构造失效时间
            Integer freezingTime = Integer.parseInt(freezeDto.getFreezingTime().toString());
            Integer days = 0;
            if (freezingTime == 1) {
                days = 3;
            } else if (freezingTime == 2) {
                days = 7;
            }
            //存入redis(冻结)
            String value = JSON.toJSONString(freezeDto);
            if (days > 0) {
                redisTemplate.opsForValue().set(key, value, days, TimeUnit.DAYS);
            } else {
                redisTemplate.opsForValue().set(key, value);
            }
        } else {
            //从redis删除(解冻)
            redisTemplate.delete(key);
        }
        return "操作成功！";
    }
}
